package com.zhengqi.qmy

import android.app.Activity
import android.app.Application
import android.app.Dialog
import android.content.Context
import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import com.songcha.library_business.config.FunctionConfig
import com.songcha.library_business.config.VarConfig
import com.songcha.library_business.util.BusinessJumpUtil
import com.songcha.library_common.util.LogUtil
import com.songcha.module_splash.ui.activity.splash.SplashActivity

class MyApplication : Application() {
    companion object{
        lateinit var mContext:Context

        @JvmStatic
        fun getContext():Context{
            return mContext
        }

        init {

        }

    }

    override fun onCreate() {
        super.onCreate()
        mContext=this

        com.songcha.library_business.LibraryBusinessApplication.init(this)

        handleActivityLifecycleCallbacks()
    }

    private var activityRef = 0
    private var activityIsChangingConfigurations = false
    private var toBackgroundTime = 0L

    private fun handleActivityLifecycleCallbacks(){

        registerActivityLifecycleCallbacks(object: ActivityLifecycleCallbacks {
            override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {
                //LogUtil.log(activity,tag="aaa onActivityCreated")

                //hasCreated = true
            }

            override fun onActivityStarted(activity: Activity) {
                //LogUtil.log(activity,tag="aaa onActivityStarted")

                //if(activity is SplashActivity) return

                LogUtil.log(activityRef,activity,tag="aaa onActivityStarted")

                //LogUtil.log(activityRef,activity,tag="aaa onActivityResumed")

                if(activityRef == 0 && !activityIsChangingConfigurations && activity !is SplashActivity){
                    onAppForeGround(((System.currentTimeMillis() - toBackgroundTime) / 1000).toInt())
                }

                activityRef++
            }

            override fun onActivityResumed(activity: Activity) {

            }

            override fun onActivityPaused(activity: Activity) {
                //LogUtil.log(activity,tag="aaa onActivityPaused")
            }

            override fun onActivityStopped(activity: Activity) {
                //if(activity is SplashActivity) return
                //TODO
                activityRef--
                activityIsChangingConfigurations = activity.isChangingConfigurations

                LogUtil.log(activityRef,activity,tag="aaa onActivityStopped")

                //LogUtil.log(activityRef,activity,tag="aaa onActivityStopped")

                if(activityRef == 0 && !activityIsChangingConfigurations && activity !is SplashActivity){
                    toBackgroundTime = System.currentTimeMillis()
                    onAppBackGround()
                }
            }

            override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {
                //LogUtil.log(activity,tag="aaa onActivitySaveInstanceState")
            }

            override fun onActivityDestroyed(activity: Activity) {
                //LogUtil.log(activity,tag="aaa onActivityDestroyed")
            }

        })
    }

    private fun hasOpenedDialogs(activity: FragmentActivity): Boolean {
        val fragments: List<Fragment> = activity.supportFragmentManager.fragments
        if (fragments != null) {
            for (fragment in fragments) {
                if (fragment is DialogFragment) {
                    return true
                }
            }
        }

        return false
    }

    private fun isDialogShowing(activity: Activity): Boolean {
        val root = activity.window.decorView.rootView as ViewGroup
        for (i in 0 until root.getChildCount()) {
            val child: View = root.getChildAt(i)
            if (child is Dialog) {
                return true
            }
        }
        return false
    }

    private fun onAppForeGround(inBackgroundSecond:Int){
        if(!FunctionConfig.IS_ADVERT_ENABLED || VarConfig.ADVERT_SPLASH_INFO.type == VarConfig.ADVERT_TYPE_DIS_ENABLED || VarConfig.HOT_START_APP_TO_SPLASH_TIME < 0 || inBackgroundSecond < VarConfig.HOT_START_APP_TO_SPLASH_TIME) return
        BusinessJumpUtil.toSplashActivity(this,true)
    }

    private fun onAppBackGround(){

    }

}